import tkinter as tk
import tkinter.messagebox
from tkinter import *
import math

def StandXC(a): #标准化脚长
    b=math.floor(a)
    if a-b>0.5:
        c=b+1
    elif a-b==0:
        c=b
    else:
        c=b+0.5
    return c


def ButtonClick():
    try:
        a=float(FootT.get())
        if a>0:    
            c=StandXC(a)
            d=int(c*2-10)
            Xlabel.config(text='适合你的鞋码为：'+str(d))
            # 加载listbox中的项
            Rlist.delete(0,END)
            SL=0
            for i in range(-4,5):
                m=c+0.5*i
                if m>=5:
                    Rlist.insert(END,"     "+str(m)+"          "+str(int(m*2-10)))
                    SL=SL+1
                    if i==0:
                        actindex=SL-1
            Rlist.select_set(actindex)
                        
                    
        else:
            tk.messagebox.showwarning("温馨提示","脚长不能为负数！")
    except ValueError:
        tk.messagebox.showwarning("温馨提示","你输入的数据有误！")
    
        

#创建窗体
mywin=tk.Tk()
mywin.wm_attributes('-topmost',1)
mywin.title("鞋码换算")
mywin.geometry('400x250')

#创建控件
FootL=tk.Label(mywin,text='你的脚长(厘米)')
FootT=tk.Entry(mywin)
Rlabel=tk.Label(mywin,text='脚长        鞋码')
Cbutton=tk.Button(mywin,text="换算",width=15,command=ButtonClick)
Xlabel=tk.Label(mywin,text='适合你的鞋码为：')
Rlist=tk.Listbox(mywin)
#布局
FootL.grid(row=0,column=0,pady=5)
FootT.grid(row=0,column=1,pady=5)
Rlabel.grid(row=0,column=2,padx=30)
Cbutton.grid(row=1,columnspan=2,pady=40)
Xlabel.grid(row=2,columnspan=2)
Rlist.grid(row=1,column=2,columnspan=2,padx=15)

mywin.mainloop()
